package library.and.xps.com.step.view.base;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.ViewHolder;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.realm.Realm;
import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.User;
import library.and.xps.com.step.util.AppCommond;
import library.and.xps.com.step.util.TeamHelper;

public abstract class BaseActivity extends CheckPermissionsActivity implements Handler.Callback, DialogInterface.OnDismissListener {
    protected AlertDialog loadingDialog;
    protected Realm realm;
    protected Handler bkThreadHandler;
    protected Handler mainThreadHandler;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
        AlertDialog.Builder ABuilder = new AlertDialog.Builder(BaseActivity.this, R.style.ProgreessDialog);
        loadingDialog = ABuilder.setView(view).setCancelable(false).create();
        loadingDialog.setOnDismissListener(this);

        try {
            realm = Realm.getDefaultInstance();
        } catch (IllegalStateException ill) {
            ill.printStackTrace();
        }

        HandlerThread handlerThread = new HandlerThread("Activity_BackgroundThreadHandler");
        handlerThread.start();
        bkThreadHandler = new Handler(handlerThread.getLooper(), this);
        mainThreadHandler = new Handler(getMainLooper(), this);
        init();

        setData();
        
    }

    protected User getUser() {
        User user;
        if (realm != null) {
            user = realm.where(User.class).findFirst();
            return user;
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T r2v(View view, int resId) {
        return (T) view.findViewById(resId);
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T r2v(int resId) {
        return (T) findViewById(resId);
    }

    public void showDialogNoCancel() {
        if (!loadingDialog.isShowing()) {
            loadingDialog.setCancelable(false);
            loadingDialog.show();
        }
    }

    public void showDialog() {
        if (!loadingDialog.isShowing()) {
            loadingDialog.setCancelable(true);
            loadingDialog.show();
        }
    }

    public void dissmisDialog() {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    protected abstract int getLayout();

    protected abstract void setData();

    protected abstract void init();

    protected void dialogDismiss() {
    }

    public Realm getmRealm(){
        return realm;
    }

    @Override
    public boolean handleMessage(Message msg) {
        return false;
    }

    public void showHintDialog(String hintText, OnClickListener listener) {
        ViewHolder contentViewHolder = new ViewHolder(getLayoutInflater().inflate(R.layout.dialog_hintview, null));
        View contentView = contentViewHolder.getInflatedView();
        ViewHolder footerViewHolder = new ViewHolder(getLayoutInflater().inflate(R.layout.footer, null));
        View footerView = footerViewHolder.getInflatedView();

        ((TextView) contentView.findViewById(R.id.tv_hint)).setText(hintText);
        DialogPlus dialog = DialogPlus.newDialog(this)
                .setContentHolder(contentViewHolder)
                .setGravity(Gravity.CENTER)
                .setFooter(footerView)
                .setOnClickListener(listener)
                .setHeader(R.layout.header)
                .setInAnimation(R.anim.dialog_in_anim)
                .setOutAnimation(R.anim.dialog_out_anim)
                .create();

        dialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        dialogDismiss();
    }
}

